package com.xiao.gulimall.product.service.impl;

import com.xiao.gulimall.product.vo.Category2Vo;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiao.common.utils.PageUtils;
import com.xiao.common.utils.Query;

import com.xiao.gulimall.product.dao.CategoryDao;
import com.xiao.gulimall.product.entity.CategoryEntity;
import com.xiao.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    /**\
     * 三级分类
     * @return
     */
    @Override
    public List<CategoryEntity> listotree() {
        //查询出所有的数据
        List<CategoryEntity> list = baseMapper.selectList(null);
        //筛选出 所有的一级分类
        List<CategoryEntity> collect = list.stream().filter(item -> {
            return item.getParentCid().equals(0l);
        }).map(pojo -> {

            //给二级分类 赋值
            pojo.setChildren(getChildren(pojo, list));
            return pojo;
        }).collect(Collectors.toList());
        return collect;
    }

    private List<CategoryEntity> getChildren(CategoryEntity pojo, List<CategoryEntity> list) {
        List<CategoryEntity> collect = list.stream().filter(item -> {
            return pojo.getCatId().equals(item.getParentCid());
        }).map(l2 -> {
            //设置三级分类 并且复制
            l2.setChildren(getChildren(l2, list));
            return l2;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public Map<Long, List<Category2Vo>> catelog() {
        List<CategoryEntity> l1List = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));

        Map<Long,List<Category2Vo>> map = new HashMap();

        for(CategoryEntity l1 : l1List) {
            List<CategoryEntity> l2List = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", l1.getCatId()));
            List<Category2Vo> l2VoList = l2List.stream().map(l2 -> {
                Category2Vo catalog2Vo = new Category2Vo();
                catalog2Vo.setCatalog1Id(l1.getCatId());
                catalog2Vo.setId(l2.getCatId());
                catalog2Vo.setName(l2.getName());
                List<CategoryEntity> l3List = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", l2.getCatId()));
                List<Category2Vo.Category3Vo> collect = l3List.stream().map(l3 -> {
                    Category2Vo.Category3Vo catalog3Vo = new Category2Vo.Category3Vo();
                    catalog3Vo.setName(l3.getName());
                    catalog3Vo.setId(l3.getCatId());
                    catalog3Vo.setCatalog2Id(l2.getCatId());
                    return catalog3Vo;
                }).collect(Collectors.toList());
                catalog2Vo.setCatalog3List(collect);
                return catalog2Vo;
            }).collect(Collectors.toList());
            map.put(l1.getCatId(),l2VoList);
        }
        return map;
    }

}